package com.konsung.ksdc.manager.shop;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.CartDetailNetBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 购物车确认订单商品列表adapter
 * Created by Administrator on 2016/5/3 0003.
 */
public class ConfirmBuyListAdapter extends BaseAdapter {

    private List<CartDetailNetBean> cartDetailNetBeanList;

    /**
     * 构造方法
     * @param list 购物车商品列表集合
     */
    public ConfirmBuyListAdapter(List<CartDetailNetBean> list) {
        cartDetailNetBeanList = list;
    }

    @Override
    public int getCount() {
        if (cartDetailNetBeanList != null) {
            return cartDetailNetBeanList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return cartDetailNetBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final CartDetailNetBean bean = cartDetailNetBeanList.get(position);

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(MyApplication.getContext())
                    .inflate(R.layout.item_product_for_confirm_order, null);
            ButterKnife.bind(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String iconImageUrl = bean.getIconImageUrl();
        Picasso.with(MyApplication.getContext())
                .load(StringUtils.appendPicPath(iconImageUrl))
                .placeholder(R.drawable.pic_talkbox_sel)
                .error(R.drawable.pic_talkbox_sel)
                .into(holder.ivProduct);

        String title = bean.getProductName();
        holder.tvTitle.setText(title);

        int count = Integer.parseInt(bean.getShopNumber());
        holder.tvNum.setText("" + count);
        String type = bean.getSubTitle();
        holder.tvSubtitle.setText(type);

        String price = bean.getMemberPrice();
        holder.tvPrice.setText("￥" + price);
        holder.tvRequestState.setVisibility(View.GONE);
        return convertView;
    }

    /**
     * 适配器
     */
    static class ViewHolder {
        @Bind(R.id.iv_product)
        ImageView ivProduct;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_num)
        TextView tvNum;
        @Bind(R.id.tv_request_state)
        TextView tvRequestState;
    }

    /**
     * 手动设置数据
     * @param data 购物车商品列表
     */
    public void setDatas(List<CartDetailNetBean> data) {
        cartDetailNetBeanList = data;
        notifyDataSetChanged();
    }
}
